using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class 剧情演出系统 : MonoBehaviour
{
    [Header("组件")]
    public Image Background;
    public SpriteRenderer Actor1;    //角色位置1 左侧
    public SpriteRenderer Actor2;    //角色位置2 右侧
    public SpriteRenderer Actor3;    //角色位置3 中央
    public Image mask;      //遮罩
    
    private bool isFading = false;
    
    [Header("备选内容")]
    [SerializeField] private List<BackgroundSO> BackgroundList = new List<BackgroundSO>(); // 修正拼写
    [SerializeField] private List<CharacterSO> characterList = new List<CharacterSO>();

    void Start()
    {
        mask.gameObject.SetActive(false);
        mask.color = Color.black;
        
        Background.gameObject.SetActive(false);
        Actor1.gameObject.SetActive(false);
        Actor2.gameObject.SetActive(false);
        Actor3.gameObject.SetActive(false);
    }
    
    public void SetImage(string position, string Name)
    {
        foreach (var character in characterList)
        {
            if (character.name == Name)
            {
                switch (position)
                {
                    case "left":
                        Actor1.sprite = character.image;
                        Actor1.gameObject.SetActive(true);
                        StartCoroutine(FadeSprite(Actor1, 0, 1, 1f)); // 使用Sprite版本
                        break; 
                    case "right":
                        Actor2.sprite = character.image;
                        Actor2.gameObject.SetActive(true);
                        StartCoroutine(FadeSprite(Actor2, 0, 1, 1f));
                        break;
                    case "mid":
                        Actor3.sprite = character.image;
                        Actor3.gameObject.SetActive(true);
                        StartCoroutine(FadeSprite(Actor3, 0, 1, 1f));
                        break;
                    case "background":
                        SetBackground(name);
                        break;
                    default:
                        Debug.LogError($"No such position: {position}");
                        break;
                }
                return;
            }
        }
        Debug.LogError($"{position}:{Name} not found");
    }
    
    // 修复背景设置方法
    public void SetBackground(string name)
    {
        foreach (var bg in BackgroundList)
        {
            if(bg.name == name) // 假设BackgroundSO有name属性
            {
                Background.sprite = bg.image; // 假设有image属性
                Background.gameObject.SetActive(true);
                StartCoroutine(FadeUI(Background, 0, 1, 1f)); // 添加淡入效果
                return;
            }
        }
        Debug.LogError($"Background {name} not found");
    }
    
    // 修复淡出方法
    public void FadeImage(string position)
    {
        switch (position)
        {
            case "left":
                StartCoroutine(FadeSprite(Actor1, 1, 0, 1f));
                break;
            case "right":
                StartCoroutine(FadeSprite(Actor2, 1, 0, 1f));
                break;
            case "mid":
                StartCoroutine(FadeSprite(Actor3, 1, 0, 1f)); // 修正参数
                break;
            case "background":
                StartCoroutine(FadeUI(Background, 1, 0, 1f)); // 添加淡出效果
                break;
            default:
                Debug.LogError($"No such position:{position}");
                break;
        }
    }

    // 修复场景淡入淡出
    public void FadeScene()
    {
        StartCoroutine(DoFadeScene());
    }

    private IEnumerator DoFadeScene()
    {
        mask.gameObject.SetActive(true);
        yield return FadeUI(mask, 0, 1, 1f); // 淡入
        yield return FadeUI(mask, 1, 0, 1f);  // 淡出
        mask.gameObject.SetActive(false);
    }
    
    // 专门处理UI元素的淡入淡出 (Image)
    private IEnumerator FadeUI(Image image, float startAlpha, float endAlpha, float duration)
    {
        if (image == null) yield break;
        
        image.gameObject.SetActive(true);
        float elapsed = 0f;
        Color color = image.color;
        color.a = startAlpha;
        image.color = color;

        isFading = true;
        
        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            float currentAlpha = Mathf.Lerp(startAlpha, endAlpha, elapsed / duration);
            color.a = currentAlpha;
            image.color = color;
            yield return null;
        }
    
        color.a = endAlpha;
        image.color = color;
    
        if (Mathf.Approximately(endAlpha, 0f)) 
        {
            image.gameObject.SetActive(false);
        }
        
        isFading = false;
    }
    
    // 新增：专门处理SpriteRenderer的淡入淡出
    private IEnumerator FadeSprite(SpriteRenderer sprite, float startAlpha, float endAlpha, float duration)
    {
        if (sprite == null) yield break;
        
        sprite.gameObject.SetActive(true);
        float elapsed = 0f;
        Color color = sprite.color;
        color.a = startAlpha;
        sprite.color = color;

        isFading = true;
        
        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            float currentAlpha = Mathf.Lerp(startAlpha, endAlpha, elapsed / duration);
            color.a = currentAlpha;
            sprite.color = color;
            yield return null;
        }
    
        color.a = endAlpha;
        sprite.color = color;
    
        if (Mathf.Approximately(endAlpha, 0f)) 
        {
            sprite.gameObject.SetActive(false);
        }
        
        isFading = false;
    }
}

